<?php
namespace PsiFramework\Plugins\Emailing\Classes\DAO;

use PsiFramework\Applications\DefaultApp\Classes\Model\User;

use PsiFramework\Plugins\Emailing\Classes\Model\EmailVariable;

use PsiFramework\Applications\DefaultApp\Classes\Model\Page;

use PsiFramework\Classes\Util\Object\ReferenceAttribute;

use PsiFramework\Plugins\Emailing\Classes\Model\EmailCampaign;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Classe de manipulation d'une campagne d'e-mail
 *
 * @package PsiFramework.Plugins.Emailing.Classes.DAO
 */
class EmailCampaignDAO
{
    /**
     * 
     * @param EmailCampaign $emailCampaign
     * 
     * @return \DOMDocument
     */
    public static function getAsXml(EmailCampaign $emailCampaign)
    {
        
        $doc = new \DOMDocument('1.0', 'UTF-8');
        $doc->formatOutput = true;
        
        $usedAtts = array('firstName', 'surName', 'email');
        
        $campaignXml = $doc->createElement('mailing');
        
        //-----------------------------------------------------
        //----------------------Sender-------------------------
        //-----------------------------------------------------
        $senderXml = $doc->createElement('sender', $emailCampaign->getSender());
        $campaignXml->appendChild($senderXml);
        
        //-----------------------------------------------------
        //---------------------Message-------------------------
        //-----------------------------------------------------
        $messageXml = $doc->createElement('message');
        $messageXml->setAttribute('lang', $emailCampaign->getMessage()->getLanguage());
        
        $messageTitleXml = $doc->createElement('title', $emailCampaign->getMessage()->getTitle());
        $messageXml->appendChild($messageTitleXml);
        
        $messageContentXml = $doc->createElement('content', $emailCampaign->getMessage()->getContent());
        $messageXml->appendChild($messageContentXml);
        
        $campaignXml->appendChild($messageXml);
        
        //-----------------------------------------------------
        //---------------------Variables-----------------------
        //-----------------------------------------------------
        $variables = $emailCampaign->getVariables();
        if (!is_null($variables) && is_array($variables) && count($variables)>0) {
            
            $variablesXml = $doc->createElement('variables');
            
            foreach ($variables as $variable) {
                $variableXml = $doc->createElement('variable');
                
                $variableXml->setAttribute('key', $variable->getKey());
                
                $variableValue = $variable->getValue();
                if ($variableValue instanceof ReferenceAttribute) {
                    $att = $variableValue->getAttribute();
                    
                    $variableXml->setAttribute('ref', $att);
                    
                    if (!in_array($att, $usedAtts)) {
                        $usedAtts[] = $att;
                    }
                } else {
                    $variableXml->setAttribute('value', $variableValue);
                }
                
                $variablesXml->appendChild($variableXml);
            }
            
            $campaignXml->appendChild($variablesXml);
        }
        
        //-----------------------------------------------------
        //--------------------Recipients-----------------------
        //-----------------------------------------------------
        $recipients = $emailCampaign->getRecipients();
        if (!is_null($recipients) && is_array($recipients) && count($recipients)>0) {
            
            $recipientsXml = $doc->createElement('recipients');
            
            foreach ($recipients as $recipient) {
                $recipientXml = $doc->createElement('recipient');
            
                foreach ($usedAtts as $att) {
                    $getter = 'get'.ucfirst($att);
                    $attXml = $doc->createElement($att, $recipient->{$getter}());
                    $recipientXml->appendChild($attXml);
                }
                
                $recipientsXml->appendChild($recipientXml);
            }
            
            $campaignXml->appendChild($recipientsXml);
            
        }
        
        $doc->appendChild($campaignXml);
        
        return $doc;
    }
    
    /**
     * 
     * @param DOMElement $campaignXml
     * 
     * @return \PsiFramework\Plugins\Emailing\Classes\Model\EmailCampaign
     */
    public static function retrieveFromXml(\DOMElement $campaignXml)
    {
        
        $emailCampaign = new EmailCampaign();
        
        $userAtts = array('firstName', 'surName', 'email');
        
        //-----------------------------------------------------
        //----------------------Sender-------------------------
        //-----------------------------------------------------
        $sender = getContentByTagName($campaignXml, 'sender');
        $emailCampaign->setSender($sender);
        
        
        //-----------------------------------------------------
        //---------------------Message-------------------------
        //-----------------------------------------------------
        $messageXml = getElementByTagName($campaignXml, 'message');
        $lang = $messageXml->getAttribute('lang');
        $messageTitle = getContentByTagName($messageXml, 'title');
        $messageContent = getContentByTagName($messageXml, 'content');
        
        $message = new Page(null, null, $lang, $messageTitle, $messageContent);
        $emailCampaign->setMessage($message);
        
        
        //-----------------------------------------------------
        //---------------------Variables-----------------------
        //-----------------------------------------------------
        $variablesXml = getElementByTagName($campaignXml, 'variables');
        $variableEls = $variablesXml->getElementsByTagName('variable');
        $userClass = get_class(new User());
        if (!is_null($variableEls)) {
            for ($i=0; $i<$variableEls->length; $i++) {
                $variableEl = $variableEls->item($i);
                $variable = new EmailVariable();
                
                $key = $variableEl->getAttribute('key');
                
                $value = null;
                if ($variableEl->hasAttribute('value')) {
                    $value = $variableEl->getAttribute('value');
                } else if ($variableEl->hasAttribute('ref')) {
                    $ref = $variableEl->getAttribute('ref');
                    
                    $value = new ReferenceAttribute();
                    $value->setObjectType($userClass);
                    $value->setAttribute($ref);
                    
                    if (!in_array($ref, $userAtts)) {
                        $userAtts[] = $ref;
                    }
                }
                
                $variable->setKey($key);
                $variable->setValue($value);
                
                $emailCampaign->addVariable($variable);
            }
        }
        
        
        //-----------------------------------------------------
        //--------------------Recipients-----------------------
        //-----------------------------------------------------
        $recipientsXml = getElementByTagName($campaignXml, 'recipients');
        $recipientEls = $recipientsXml->getElementsByTagName('recipient');
        if (!is_null($recipientEls)) {
            for ($i=0; $i<$recipientEls->length; $i++) {
                $recipientEl = $recipientEls->item($i);
                
                $recipient = new User();
                foreach ($userAtts as $userAtt) {
                    $setter = 'set'.ucfirst($userAtt);
                    $attValue = getContentByTagName($recipientEl, $userAtt);
                    
                    $recipient->{$setter}($attValue);
                }
                
                $emailCampaign->addRecipient($recipient);
            }
        }
        
        
        return $emailCampaign;
    }
}